package com.chenfan.magic.interpreter;

import com.chenfan.common.config.Constant;
import com.chenfan.common.exception.BusinessException;
import com.chenfan.common.vo.UserVO;
import com.chenfan.magic.config.UserVoConstextHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author chenm
 * @title: FeignHeaderInterceptor
 * @projectName mini-magic
 * @description: 远程接口调用拦截器
 * @date 2022/4/19 9:58
 */
@Component
public class FeignHeaderInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        UserVO userVo = UserVoConstextHolder.getUserVo();
        if (Objects.isNull(userVo) || StringUtils.isBlank(userVo.getToken())){
            throw new BusinessException(Constant.UNAUTHORIZED, "用户登录信息失效");
        }
        template.header(HttpHeaders.AUTHORIZATION, userVo.getToken());
    }
}
